//
//  ZCChartsViewController.m
//  VoiceMusic
//
//  Created by dllo on 15/10/16.
//  Copyright © 2015年 zhangdeyu. All rights reserved.
//

#import "ZCChartsViewController.h"
#import "ZCChartsModel.h"
#import "ZCChartsViewController.h"
#import "ZCAFNetWork.h"
#import "ZCChartsTableViewCell.h"
#import "ZCDeListViewController.h"
#import <UIImageView+WebCache.h>
#import <Masonry.h>
@interface ZCChartsViewController ()<UITableViewDataSource,UITableViewDelegate>
@property (nonatomic, strong) UITableView *chartsTableView;
@property (nonatomic, strong) NSMutableArray *sectionNameArr;
@property (nonatomic, strong) NSMutableArray *titleArr;
@property (nonatomic, strong) NSMutableArray *titleSecondArr;
@property (nonatomic, strong) NSMutableArray *titleThirdArr;
@property (nonatomic, strong) NSMutableArray *titleDetailArr;
@property (nonatomic, strong) NSMutableArray *titleSecondDetailArr;
@property (nonatomic, strong) NSMutableArray *titleThirdDetailArr;

@end

@implementation ZCChartsViewController
#pragma mark 初始化
-(instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.sectionNameArr = [NSMutableArray array];
        self.titleArr = [NSMutableArray array];
        self.titleSecondArr = [NSMutableArray array];
        self.titleThirdArr = [NSMutableArray array];
        self.titleDetailArr = [NSMutableArray array];
        self.titleSecondDetailArr = [NSMutableArray array];
        self.titleThirdDetailArr = [NSMutableArray array];
    }
    return self;
}
#pragma mark 代理置空
- (void)dealloc
{
    _chartsTableView.dataSource = nil;
    _chartsTableView.delegate = nil;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"750365_105833058_2"]];
    [self createChartsTableView];
    [self createChartsData];
    //返回键
    UIButton *leftButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [leftButton setImage:[UIImage imageNamed:@"iconfont-fanhui"] forState:UIControlStateNormal];
    leftButton.frame = CGRectMake(0, 0, 30, 30);
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:leftButton];
    [leftButton addTarget:self action:@selector(returnClick:) forControlEvents:UIControlEventTouchUpInside];
}
#pragma mark 返回
- (void)returnClick:(UIBarButtonItem *)button
{
    [self.navigationController popViewControllerAnimated:YES];
}
#pragma mark 创建排行榜tableView视图
- (void)createChartsTableView
{
    self.chartsTableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStyleGrouped];
    [self.view addSubview:self.chartsTableView];
    self.chartsTableView.backgroundColor = [UIColor clearColor];
    [self.chartsTableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.right.equalTo(self.view);
        make.bottom.equalTo(self.view.mas_bottom).offset(-49);
    }];
    self.chartsTableView.delegate = self;
    self.chartsTableView.dataSource = self;
    self.chartsTableView.emptyDataSetDelegate = self;
    self.chartsTableView.emptyDataSetSource = self;
    self.chartsTableView.rowHeight = 150;
}
#pragma mark 创建排行榜数据
- (void)createChartsData
{
    [ZCAFNetWork getUrl:@"http://api.songlist.ttpod.com/channels/bhb/children?v=v8.1.0.2015071519" body:nil result:ZCJSON headerFile:nil success:^(id result) {
        NSArray *arr = result;
        // 分区头标题数组
        self.sectionNameArr = [ZCChartsModel baseModelByArr:arr];
        // 分区数组
        if (arr.count != 0) {
            NSDictionary *dic = arr[0];
            self.titleArr = [ZCChartsModel baseModelByArr:dic[@"refs"]];
            [self.titleArr removeObjectAtIndex:0];
            
            NSDictionary *dicS = arr[1];
            self.titleSecondArr = [ZCChartsModel baseModelByArr:dicS[@"refs"]];
            NSDictionary *dicT = arr[2];
            self.titleThirdArr = [ZCChartsModel baseModelByArr:dicT[@"refs"]];
        }

        [self.chartsTableView reloadData];
    } failure:^(NSError *error) {
        NSLog(@"error == %@",error);
    }];
}
#pragma mark 分区数
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return self.sectionNameArr.count;
}
#pragma mark tableView分区行数
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (section == 0) {
        return self.titleArr.count;
    }else if (section == 1)
    {
        return self.titleSecondArr.count;
    }else
    {
        return self.titleThirdArr.count;
    }
}
#pragma mark 分区标题
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    ZCChartsModel *model = self.sectionNameArr[section];
    return model.name;
}
#pragma mark cell赋值
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *reuse=@"reuse";
    ZCChartsTableViewCell *cell =[tableView dequeueReusableCellWithIdentifier:reuse];
    if (!cell) {
        cell =[[ZCChartsTableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:reuse];
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.backgroundColor = [UIColor clearColor];
    if (indexPath.section == 0) {
        ZCChartsModel *titleModel = self.titleArr[indexPath.row];
        cell.titleLabel.text = titleModel.title;
        NSString *picUrl = titleModel.image[@"pic"];
        [cell.picImageView sd_setImageWithURL:[NSURL URLWithString:picUrl]placeholderImage:[UIImage imageNamed:@"3187"]];
        NSMutableArray *songsArr = titleModel.songs;
        NSString *Ftext = [NSString stringWithFormat:@"%@ - %@",songsArr[0][@"name"],songsArr[0][@"singerName"]];
        cell.firstLabel.text = Ftext;
        
        NSString *Stext = [NSString stringWithFormat:@"%@ - %@",songsArr[1][@"name"],songsArr[1][@"singerName"]];
        cell.secondLabel.text = Stext;
        
        NSString *Ttext = [NSString stringWithFormat:@"%@ - %@",songsArr[2][@"name"],songsArr[2][@"singerName"]];
        cell.thirdLabel.text = Ttext;
        

    }else if (indexPath.section == 1)
    {
        ZCChartsModel *titleModel = self.titleSecondArr[indexPath.row];
        cell.titleLabel.text = titleModel.title;
        NSString *picUrl = titleModel.image[@"pic"];
        [cell.picImageView sd_setImageWithURL:[NSURL URLWithString:picUrl]placeholderImage:[UIImage imageNamed:@"3187"]];
        NSMutableArray *songsArr = titleModel.songs;
        NSString *Ftext = [NSString stringWithFormat:@"%@ - %@",songsArr[0][@"name"],songsArr[0][@"singerName"]];
        cell.firstLabel.text = Ftext;
        
        NSString *Stext = [NSString stringWithFormat:@"%@ - %@",songsArr[1][@"name"],songsArr[1][@"singerName"]];
        cell.secondLabel.text = Stext;
        
        NSString *Ttext = [NSString stringWithFormat:@"%@ - %@",songsArr[2][@"name"],songsArr[2][@"singerName"]];
        cell.thirdLabel.text = Ttext;
    }else
    {
        ZCChartsModel *titleModel = self.titleThirdArr[indexPath.row];
        cell.titleLabel.text = titleModel.title;
        NSString *picUrl = titleModel.image[@"pic"];
        [cell.picImageView sd_setImageWithURL:[NSURL URLWithString:picUrl]placeholderImage:[UIImage imageNamed:@"3187"]];
        NSMutableArray *songsArr = titleModel.songs;
        NSString *Ftext = [NSString stringWithFormat:@"%@ - %@",songsArr[0][@"name"],songsArr[0][@"singerName"]];
        cell.firstLabel.text = Ftext;
        
        NSString *Stext = [NSString stringWithFormat:@"%@ - %@",songsArr[1][@"name"],songsArr[1][@"singerName"]];
        cell.secondLabel.text = Stext;
        
        NSString *Ttext = [NSString stringWithFormat:@"%@ - %@",songsArr[2][@"name"],songsArr[2][@"singerName"]];
        cell.thirdLabel.text = Ttext;
    }
    return cell;
}
#pragma mark tableView点击
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    ZCDeListViewController *detailListVC = [[ZCDeListViewController alloc] init];
    if (indexPath.section == 0) {
        ZCChartsModel *titleModel = self.titleArr[indexPath.row];
        detailListVC.myId = titleModel.songlist_id;
    }else if (indexPath.section == 1)
    {
        ZCChartsModel *titleModel = self.titleSecondArr[indexPath.row];
        detailListVC.myId = titleModel.songlist_id;
    }else
    {
        ZCChartsModel *titleModel = self.titleThirdArr[indexPath.row];
        detailListVC.myId = titleModel.songlist_id;
    }
    
    
    [self.navigationController pushViewController:detailListVC animated:YES];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
